package com.xxl.job.executor.service.jobhandler;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.bizsys.service.ILoanIouService;
import com.ruoyi.bizsys.vo.LoanIouVo;
import com.ruoyi.market.config.MarketUserConfig;
import com.ruoyi.market.util.HttpConnetUtil;
import com.ruoyi.system.service.ISysDictDataService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Author: ygw
 * @Date: 2022/05/05/11:58
 */
@Slf4j
@Component
public class IouSyncToMarketJob {

    @Autowired
    private ILoanIouService loanIouService;

    @Autowired
    MarketUserConfig marketUserConfig;

    @Autowired
    ISysDictDataService sysDictDataService;

    private static final String IOU_INTERVAL_HOURS = "iou_intervalHours";

    //定时查询生成的IOU订单
    @XxlJob("iouSyncMarketJobHandler")
    public void execute() {
        XxlJobHelper.log("IouSyncToMarketJob Start>>>>>>>>>>");

        //查询近以下是生成的iou订单
        List<LoanIouVo> loanIouVoList = loanIouService.selectIouTask(sysDictDataService.selectDictValue(IOU_INTERVAL_HOURS));
        JSONObject jsonParam = marketUserFormat(loanIouVoList);

        //推送至营销系统
        if (jsonParam.getJSONArray("data").size() <= 0) {
            XxlJobHelper.log("无待处理数据");
            XxlJobHelper.log("IouSyncToMarketJob End >>>>>>>>>>");
            return;
        }

        String resultInfo = null;
        try {
            XxlJobHelper.log(jsonParam.toJSONString());
            resultInfo = HttpConnetUtil.sendPost(marketUserConfig.getMarketIouImportUrl(), jsonParam.toJSONString());
        } catch (Exception e) {
            XxlJobHelper.log("推送营销系统异常");
            XxlJobHelper.log(e);
            XxlJobHelper.handleFail("推送营销系统异常");
        }
        XxlJobHelper.log("成功推送至营销系统： " + resultInfo);

        XxlJobHelper.log("IouSyncToMarketJob End >>>>>>>>>>");
    }

    public JSONObject marketUserFormat(List<LoanIouVo> loanIouVoList) {
        JSONObject jsonParam = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (LoanIouVo loanIouVo : loanIouVoList) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("loanAmt", loanIouVo.getLoanAmt());
            jsonObject.put("phone", loanIouVo.getPhone());
            jsonObject.put("loanSerialNo", loanIouVo.getLoanSerialNo());
            jsonObject.put("custNo", loanIouVo.getCustNo());
            jsonObject.put("createTime", sdf.format(loanIouVo.getCreateTime()));
            jsonObject.put("userUuid", loanIouVo.getUserUuid());
            jsonObject.put("userName", loanIouVo.getUserName());
            jsonObject.put("orderCreateTime", sdf.format(loanIouVo.getOrderCreateTime()));
            jsonArray.add(jsonObject);
        }
        jsonParam.put("data", jsonArray);
        return jsonParam;
    }
}

